package com.berry.MedCheck.grapy;

import android.content.Context;
import android.graphics.*;
import android.util.AttributeSet;
import android.util.Log;
import android.widget.Toast;
import com.berry.MedCheck.R;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MAChart extends GridChart {
	private final String TAG = "MAChart";
	private List<LineEntity> lineData;
	private Path mPathLine1;
	private Path mPathDivide;
	private Path mPathShadow1;
	private Paint mPaintLine;
	private Paint mPaintDivide;
	private Paint mPaintShadow;
	private List<Map<String, Float>> mPath1Value;
	private int maxPointNum;
	private int minValue;
	private int maxValue;
	private int myX;
	private int myY;
	private int myDX;
	private int myDY;
	private float BOARD_ROUND ;
	private int mDivideIndex = 0;
	private boolean bStop = false;
	private float mYExceed;
	ArrayList<HashMap<String, Object>> listItem;
	private float mDividLinePos;

	public MAChart(Context context) {
		super(context);
	}

	public MAChart(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
	}

	public MAChart(Context context, AttributeSet attrs) {
		super(context, attrs);
	}

	public void swapLines() {
		//bDrawLine2 = !bDrawLine2;
	}

	public void setDivideIndex(int index) {
		mDivideIndex = index;
	}

	// //////////方�?//////////////
	@Override
	public void draw(Canvas canvas) {
		super.draw(canvas);
	}

	private void checkPathAndPaints() {
		if (mPathShadow1 == null) {
			mPathShadow1 = new Path();
		}

		if (mPathLine1 == null) {
			mPathLine1 = new Path();
		}
		if (mPathDivide == null) {
			mPathDivide = new Path();
		}
		if (mPaintLine == null) {
			mPaintLine = new Paint();
			mPaintLine.setColor(Color.WHITE);
			mPaintLine.setAntiAlias(true);
			mPaintLine.setDither(true);
			mPaintLine.setStrokeWidth(4);
			mPaintLine.setStrokeCap(Paint.Cap.ROUND);
			mPaintLine.setStrokeJoin(Paint.Join.ROUND);
			mPaintLine.setStyle(Paint.Style.STROKE);
		}
		if (mPaintDivide == null) {
			mPaintDivide = new Paint();
			mPaintDivide.setColor(0XFF7799CE);
			mPaintDivide.setAntiAlias(true);
			mPaintDivide.setDither(true);
			mPaintDivide.setStrokeWidth(8);
			mPaintDivide.setStrokeCap(Paint.Cap.ROUND);
			mPaintDivide.setStrokeJoin(Paint.Join.ROUND);
			mPaintDivide.setStyle(Paint.Style.STROKE);
		}
		if (mPaintShadow == null) {
			mPaintShadow = new Paint();
			mPaintShadow.setColor(0XFFADC4E4);
			mPaintShadow.setAntiAlias(true);
			mPaintShadow.setStrokeCap(Paint.Cap.ROUND);
			mPaintShadow.setStyle(Paint.Style.FILL);
			mPaintShadow.setStrokeJoin(Paint.Join.ROUND);
		}
		if (mPath1Value == null) {
			mPath1Value = new ArrayList<Map<String, Float>>();
		}
		if (mYExceed == 0) {
			mYExceed = getResources().getDimension(R.dimen.machart_y_exceed);
		}
		mYExceed = 0 ;

		if (BOARD_ROUND == 0) {
			BOARD_ROUND = getResources().getDimension(R.dimen.machart_board_round);
		}

	}

	@Override
	protected void onDraw(Canvas canvas) {
		super.onDraw(canvas);
		checkPathAndPaints();
		mPathShadow1.reset();
		mPathLine1.reset();
		mPath1Value.clear();
		if (bStop == true) {
			mPathLine1.reset();
			mPathLine1.moveTo(0, myDY + mYExceed);
			mPathLine1.lineTo(myDX, myDY + mYExceed);
			drawLine(canvas);
			return;
		}
		if (null != this.lineData) {
			preparePathValue();
			makePaths();
			drawUnderLinesShadow(canvas);
			drawLine(canvas);
			drawDivideLine(canvas);

		}

	}

	private void drawDivideLine(Canvas canvas) {
		float DX = 4;
		if (mDividLinePos + DX > myDX) {
			DX = myDX - mDividLinePos;
		}
		mPathDivide.reset();
		mPathDivide.moveTo(mDividLinePos, 10);
		mPathDivide.lineTo(mDividLinePos + DX, 10);
		mPathDivide.lineTo(mDividLinePos + DX, myDY - 2);
		mPathDivide.lineTo(mDividLinePos, myDY - 2);
		mPathDivide.close();
		canvas.drawPath(mPathDivide, mPaintDivide);

	}

	private void makePaths() {
		float YBase = myDY + mYExceed;
		mPathLine1.reset();
		mPathLine1.moveTo(0, myDY-BOARD_ROUND);
		mPathShadow1.reset();
		mPathShadow1.moveTo(0, myDY-BOARD_ROUND);

		if (mPath1Value.size() == 0) {
			return;
		}
		if (mPath1Value.size() < maxPointNum) {
			for (int i = 0; i < mPath1Value.size(); i++) {
				float x = mPath1Value.get(i).get("x");
				float y = mPath1Value.get(i).get("y");
				mPathLine1.lineTo(x, y);
				mPathShadow1.lineTo(x, y);
				if (i == (mDivideIndex - 1)) {
//					mPathLine1.lineTo(mPath1Value.get(mDivideIndex - 1).get("x"), 0);
//					mPathLine1.lineTo(mPath1Value.get(mDivideIndex - 1).get("x"), YBase);
//					mPathLine1.lineTo(x, y);
//					mPathShadow1.lineTo(mPath1Value.get(mDivideIndex - 1).get("x"), 0);
//					mPathShadow1.lineTo(mPath1Value.get(mDivideIndex - 1).get("x"), YBase);
//					mPathShadow1.lineTo(x, y);
					mDividLinePos = x;
				}
			}
		} else {
			for (int i = 0; i < mPath1Value.size(); i++) {
				float x = mPath1Value.get(i).get("x");
				float y = mPath1Value.get(i).get("y");
				mPathLine1.lineTo(x, y);
				mPathShadow1.lineTo(x, y);
				if (i == mDivideIndex) {
//					mPathLine1.lineTo(mPath1Value.get(mDivideIndex).get("x"), 0);
//					mPathLine1.lineTo(mPath1Value.get(mDivideIndex).get("x"), YBase);
//					mPathLine1.lineTo(x, y);
//					mPathShadow1.lineTo(mPath1Value.get(mDivideIndex).get("x"), 0);
//					mPathShadow1.lineTo(mPath1Value.get(mDivideIndex).get("x"), YBase);
//					mPathShadow1.lineTo(x, y);
					mDividLinePos = x;
				}
			}
		}
		float lastX = mPath1Value.get(mPath1Value.size() - 1).get("x");
		mPathShadow1.lineTo(lastX, myDY);
		mPathShadow1.arcTo(new RectF(lastX - BOARD_ROUND, myDY - BOARD_ROUND, lastX, myDY), 0, 90);
		mPathShadow1.lineTo(0, myDY);
		mPathShadow1.arcTo(new RectF(0, myDY - BOARD_ROUND, BOARD_ROUND, myDY), 90, 90);
		mPathShadow1.close();
	}

	private void drawLine(Canvas canvas) {
		//mPaintLine.setColor(Color.RED);
//			canvas.drawPath(mPathLine2, mPaintLine);
		//mPaintLine.setColor(Color.WHITE);
		canvas.drawPath(mPathLine1, mPaintLine);
	}

	public void stop() {
		bStop = true;
		invalidate();
	}

	public void start() {
		bStop = false;
		invalidate();

	}

	public boolean isStop() {
		return bStop;
	}

	@Override
	protected void onSizeChanged(int w, int h, int oldw, int oldh) {
		super.onSizeChanged(w, h, oldw, oldh);
		myDX = w;
		myDY = h;
	}

	private void drawUnderLinesShadow(Canvas canvas) {
//			canvas.drawPath(mPathShadow2, mPaintShadow);
		canvas.drawPath(mPathShadow1, mPaintShadow);

	}

	protected void preparePathValue() {
		// 点线距离
		float lineLength;
		int nDisplayPoints = getMaxPointNum();
		if (getWidth() < getMaxPointNum()) {
			lineLength = 1;
			nDisplayPoints = getWidth();
		} else {
			lineLength = (float) ((super.getWidth() - super.getAxisMarginLeft()) / this.getMaxPointNum());
		}
		float nMiddleInY = (int) ((super.getHeight() - super.getAxisMarginBottom()) / 2);
		// 起始位置
		float startX = lineLength / 2;
		//逐条输�?MA线
//		if (lineData.size() != 2) {
//			Toast.makeText(getContext(), "MAChart data error!!!",Toast.LENGTH_LONG).show();
//			return;
//		}
		LineEntity line = null;
		Path curLinePath = null;
		Path curShadowPath = null;
		List<Map<String, Float>> curPathValue = null;
		{
			line = (LineEntity) lineData.get(0);
			curLinePath = mPathLine1;
			curShadowPath = mPathShadow1;
			curPathValue = mPath1Value;
		}
		{

			List<Float> datas = line.getLineData();
			startX += super.getAxisMarginLeft();
			float valueY;
			float valueDrawY;
			if (datas == null) {
				return;
			}
			for (int j = 0; j < datas.size(); j++) {
				if (j > nDisplayPoints) {
					return;
				}
				float value = datas.get(j);
				//显示高度
				valueY = (((value - this.getMinValue())
						/ (this.getMaxValue() - this.getMinValue()))
						* (super.getHeight() - super.getAxisMarginBottom()));
				//点位置，需要用总高度减去显示高度
				valueDrawY = ((super.getHeight() - super.getAxisMarginBottom()) - valueY);
				if (valueY == 0) {
					valueDrawY +=mYExceed;
				}
				Map<String, Float> map = new HashMap<String, Float>();
				if (j == 0) {
					map.put("x", (float) 0);
					map.put("y", nMiddleInY);
				}
				map.put("x", startX);
				map.put("y", valueDrawY);
				startX = startX + lineLength;
				curPathValue.add(j, map);
				//Log.d("AAAA", "MAchart lineDx=" + lineLength + ";x=" + startX + ";y=" + valueDrawY);
			}
		}
	}

	private void fixValue(List<Float> data) {
		int SAME_VALUE = 4;
		for (int index = 0; index < data.size(); index++) {
			if (index < SAME_VALUE) {
				continue;
			}
			if (data.size() - index - 1 < SAME_VALUE) {
				break;
			}
			int nEqualValueNum = 0;
			int nFirstValueIndex = 0;
			float nBaseValue = 0;
			float nOldValue = 0;
			for (int i = index; i < data.size()-1; i++) {
				if (!data.get(i).equals(data.get(i + 1))) {
					nFirstValueIndex = i;
					nBaseValue = data.get(nFirstValueIndex+1);
					nOldValue = data.get(nFirstValueIndex);
					break;
				}
			}
			for (int i = nFirstValueIndex + 1; i < data.size(); i++) {
				if (nBaseValue == data.get(i)) {
					nEqualValueNum++;
					continue;
				}
				break;
			}
			float offset = Math.abs(data.get(nFirstValueIndex) - data.get(nFirstValueIndex + 1));
			float step = offset / (nEqualValueNum / 2);
			if (data.get(nFirstValueIndex ) < data.get(nFirstValueIndex+1)) {
				for (int i = 0; i < nEqualValueNum / 2; i++) {
					data.set(nFirstValueIndex + i, nOldValue + step * (i + 1));
				}
			} else {
				for (int i = 0; i < nEqualValueNum / 2; i++) {
					data.set(nFirstValueIndex + i, nOldValue - step * (i + 1));
				}
			}
			index+=  (nEqualValueNum / 2);


		}

	}



	public List<LineEntity> getLineData() {
		return lineData;
	}

	public void setLineData(List<LineEntity> lineData) {
		this.lineData = lineData;
	}

	public int getMaxPointNum() {
		return maxPointNum;
	}

	public void setMaxPointNum(int maxPointNum) {
		this.maxPointNum = maxPointNum;
	}

	public int getMinValue() {
		return minValue;
	}

	public void setMinValue(int minValue) {
		this.minValue = minValue;
	}

	public int getMaxValue() {
		return maxValue;
	}

	public void setMaxValue(int maxValue) {
		this.maxValue = maxValue;
	}
}
